package com.iyunwen.datatool.process;

import com.iyunwen.datatool.dao.EventDataMapper;
import com.iyunwen.datatool.model.dbdo.EventDataDO;
import com.iyunwen.datatool.model.flow.PageResult;
import com.iyunwen.datatool.model.vo.EventGroupVO;
import com.iyunwen.datatool.rule.ISignalAttentionRuleEngine;
import com.iyunwen.datatool.service.IDataLoadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 数据检测，处理流程
 * @author: Zhu.Guodong
 * @create: 2020-03-16 20:35
 * @version:
 */
@Slf4j
@Component
public class DataDetectionProcess {

    @Autowired
    IDataLoadService dataLoadService;

    @Autowired
    ISignalAttentionRuleEngine ruleEngine;

    @Autowired
    EventDataMapper eventDataMapper;


    public void dataProcessing() {
        String logFilePath = "/Users/zhuguodong/work/yunwen/devFiles/eventLog";
        String fileDirectionPath = "/Volumes/LongLongAgo/信号数据";
//        String fileDirectionPath = "D:/excel/event";
        //查找范围(毫秒)
        Long rangTime = 10000L;
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        String s = new SimpleDateFormat("yyyyMMddHHmmss").format(timestamp);
        String tableName = "event_data_"+s;
        //dataLoadService.createTable(tableName);
        //读取数据
        long readFileStartTime = System.currentTimeMillis();
        List<File> files = dataLoadService.readHtmlFile(logFilePath);
        for (File file : files) {
            List<EventDataDO> eventDataDOS = dataLoadService.readSignalDataFromHtml(file);
            //入库
            //dataLoadService.insertSignalDataToDatabase(eventDataDOS,tableName);
        }
        long readFileDurationTime = System.currentTimeMillis() - readFileStartTime;
        log.info("读取解析原始文件耗时:{}", readFileDurationTime);
        //查询入库数据。根据时间排序
        //处理的数据下标
//        long index = 0L;
//
//        //分页参数
//        int pageNum = 1;
//        int pageSize = 100;
//        //循环标识
//        boolean flag = true;
//        //分组结果存储对象
//        List<EventGroupVO> groupResult = new ArrayList<>();
//        do {
//            PageResult<EventDataDO> eventData = dataLoadService.getEventData(pageNum, pageSize);
//            List<EventDataDO> rows = eventData.getRows();
//            for (EventDataDO itemEventData : rows) {
//                //判断是否是异常数据
//                boolean attentionSignalFlag = ruleEngine.isAttentionSignal(itemEventData);
//                if (attentionSignalFlag) {
//                    //获取异常数据范围内的时间
//                    List<EventDataDO> eventDataDOList = dataLoadService.readSignalDataFromDb(itemEventData, rangTime);
//                    //生成分组数据
//                    EventGroupVO groupByEventDataAndRangeData = ruleEngine.getGroupByEventDataAndRangeData(itemEventData, eventDataDOList);
//                    //add to result
//                    groupResult.add(groupByEventDataAndRangeData);
//                }
//
//                //进度
//                index++;
//            }
//            //分页增加
//            pageNum++;
//            //是否截止
//            long endIndex = pageNum * 100;
//            if (endIndex >= eventData.getTotal()) {
//                flag = false;
//            }
//        } while (flag);
//
//        //分组完成
//        //分组去重 groupResult
//        dataLoadService.distinctEventGroup(groupResult);
//        //高等级无关键词单独分组
//        EventGroupVO highLevelSignalDataNotInGroup = dataLoadService.getHighLevelSignalDataNotInGroup();
//        groupResult.add(highLevelSignalDataNotInGroup);
//        //检索符合条件，写入Excel
//        dataLoadService.writeEventGroupDataToExcel(groupResult);

    }

}
